package com.yilu.driver.ui.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.is.common.utils.SPUtils;
import com.is.ui.widget.IconButton;
import com.yilu.driver.R;
import com.yilu.driver.base.BaseActivity;
import com.yilu.driver.entity.RegisteredTraineeResponse;
import com.yilu.driver.entity.ScroeRegisterResponse;
import com.yilu.driver.mvp.contract.ScoreRegisterContract;
import com.yilu.driver.mvp.model.ScoreRegisterModel;
import com.yilu.driver.mvp.presenter.ScoreRegisterPresenter;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 成绩登记
 *
 * @author Administrator
 * @date 2017/4/15 0015
 */

public class ScoreRegisterActivity extends BaseActivity implements ScoreRegisterContract.View {

    @Bind(R.id.ib_back)
    ImageButton ibBack;
    @Bind(R.id.ib_exit)
    ImageButton ibExit;
    @Bind(R.id.txt_title)
    TextView txtTitle;
    @Bind(R.id.ib_search)
    ImageButton ibSearch;
    @Bind(R.id.ib_menu)
    ImageButton ibMenu;
    @Bind(R.id.btn_passed)
    RadioButton btnPassed;
    @Bind(R.id.btn_failed)
    RadioButton btnFailed;
    @Bind(R.id.rg_pass)
    RadioGroup rgPass;
    @Bind(R.id.btn_missed)
    RadioButton btnMissed;
    @Bind(R.id.btn_no_miss)
    RadioButton btnNoMiss;
    @Bind(R.id.rg_miss)
    RadioGroup rgMiss;
    @Bind(R.id.txt_enrolTime)
    TextView txtEnrolTime;
    @Bind(R.id.btn_Have)
    RadioButton btnHave;
    @Bind(R.id.btn_No)
    RadioButton btnNo;
    @Bind(R.id.rg_have_car)
    RadioGroup rgHaveCar;
    @Bind(R.id.btn_score_register)
    IconButton btnScoreRegister;
    @Bind(R.id.btn_send)
    RadioButton btnSend;
    @Bind(R.id.btn_no_send)
    RadioButton btnNoSend;
    @Bind(R.id.rg_send_message)
    RadioGroup rgSendMessage;
    private ScoreRegisterPresenter presenter;
    private ScoreRegisterModel model;
    private List<RegisteredTraineeResponse.DataBean> beanList;
    private Integer operator;//操作人
    private Integer isThrough;//是否通过
    private String aboardTime;//上车时间
    private Integer coachid;//教练id
    private Integer subject;//科目
    private Integer[] subId;//学员id集合
    private Integer ismakeUpExamination;//是否补考
    private boolean note;//是否发送短信通知

    @Override
    public void initPresenter() {
        presenter = new ScoreRegisterPresenter(mContext);
        model = new ScoreRegisterModel();
        presenter.setVM(this, model);
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        beanList = extras.getParcelableArrayList("list");
        subject = extras.getInt("subject");
    }

    @Override
    public void retureScoreRegister(ScroeRegisterResponse unRegisteredTrainee) {

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_score_register;
    }

    @Override
    protected void initViewsAndEvents() {
        operator = (Integer) SPUtils.get(mContext, "id", 0);
        List<Integer> list = new ArrayList<>();
        for (RegisteredTraineeResponse.DataBean dataBean : beanList) {
            Log.e("single", dataBean.getName());
            list.add(dataBean.getId());
        }
        subId = list.toArray(subId);

        rgPass.setOnCheckedChangeListener(btnListener1);
        rgMiss.setOnCheckedChangeListener(btnListener2);
        rgHaveCar.setOnCheckedChangeListener(btnListener3);
        rgSendMessage.setOnCheckedChangeListener(btnListener4);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    public void scoreRegister(Integer operator, Integer isThrough, String aboardTime
            , Integer coachid, Integer ismakeUpExamination, Integer subject
            , Integer[] subId, boolean note) {
        JSONObject json = new JSONObject();
        try {
            json.put("operator", operator);
            json.put("isThrough", isThrough);
            json.put("aboardTime", aboardTime);
            json.put("coachid", coachid);
            json.put("ismakeUpExamination", ismakeUpExamination);
            json.put("subject", subject);
            json.put("subId", subId);
            json.put("note", note);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        presenter.scoreRegister(json);
    }

    @OnClick({R.id.ll_aboard_time, R.id.btn_score_register})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_aboard_time:
                break;
            case R.id.btn_score_register:
                Log.e("single", operator + isThrough + aboardTime + ismakeUpExamination + subject + subId.length + note);
                scoreRegister(operator, isThrough, aboardTime
                        , coachid, ismakeUpExamination, subject
                        , subId, note);
                break;
        }
    }

    public class radioGroupSelected implements RadioGroup.OnCheckedChangeListener {
        public String btnID;

        public radioGroupSelected(String str) {
            btnID = str;
        }

        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
            switch (btnID) {
                case "1":
                    if (checkedId == btnPassed.getId()) {
                        isThrough = 1;
                    } else if (checkedId == btnFailed.getId()) {
                        isThrough = 2;
                    }
                    Log.e("single", isThrough + "");
                    break;
                case "2":
                    if (checkedId == btnMissed.getId()) {
                        ismakeUpExamination = 1;
                    } else if (checkedId == btnNoMiss.getId()) {
                        ismakeUpExamination = 2;
                    }
                    Log.e("single", ismakeUpExamination + "");
                    break;
                case "3":
                    if (checkedId == btnHave.getId()) {

                    } else if (checkedId == btnNo.getId()) {

                    }
                    break;
                case "4":
                    if (checkedId == btnSend.getId()) {
                        note = true;
                    } else if (checkedId == btnNoSend.getId()) {
                        note = false;
                    }
                    Log.e("single", note + "");
                    break;
            }
        }
    }

    radioGroupSelected btnListener1 = new radioGroupSelected("1");
    radioGroupSelected btnListener2 = new radioGroupSelected("2");
    radioGroupSelected btnListener3 = new radioGroupSelected("3");
    radioGroupSelected btnListener4 = new radioGroupSelected("4");
}
